package com.company.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.AlbumEntity;
import com.company.project.service.AlbumService;
import com.company.project.service.HttpSessionService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;



/**
 * 相册管理表
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2021-12-30 11:45:44
 */
@Controller
@RequestMapping("/")
public class AlbumController {
    @Autowired
    private AlbumService albumService;
    @Autowired
    private HttpSessionService httpSessionService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/album")
    public String album() {
        return "album/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("album/add")
    @RequiresPermissions("album:add")
    @ResponseBody
    public DataResult add(@RequestBody AlbumEntity album){
        JSONObject currentSession = httpSessionService.getCurrentSession();
        System.out.println(currentSession);
        album.setCreateTime(new Date());
        album.setDeleted(1); // 未删除
        album.setTagUser(currentSession.getString("userid-key"));
        album.setCreateId(currentSession.getString("userid-key"));
        albumService.save(album);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("album/delete")
    @RequiresPermissions("album:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        albumService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("album/update")
    @RequiresPermissions("album:update")
    @ResponseBody
    public DataResult update(@RequestBody AlbumEntity album){
        albumService.updateById(album);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("album/listByPage")
    @RequiresPermissions("album:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody AlbumEntity album){
        Page page = new Page(album.getPage(), album.getLimit());
        LambdaQueryWrapper<AlbumEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AlbumEntity::getDeleted,1);
        //查询条件示例
        //queryWrapper.eq(AlbumEntity::getId, album.getId());
        IPage<AlbumEntity> iPage = albumService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
